package com.xauto.pioneer.appuser.activity.mine.receipt;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.ApplyParkingReceiptAdapter;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/** 申请停车发票
 * Created by Administrator on 2018/5/25.
 */

public class ApplyParkingReceiptActivity extends BaseActivity {
    @Bind(R.id.topBar_applyparkingreceipt)
    TopBar topBar_applyparkingreceipt;
    @Bind(R.id.tv_applyparkingreceipt_submit)
    TextView tv_applyparkingreceipt_submit;
//    @Bind(R.id.list_applyparkingreceipt)
//    PullToRefreshListView pullToRefreshListView;

    @Bind(R.id.list_applyparkingreceipt)
    ListView listView;
    ApplyParkingReceiptAdapter adapter;
    private List<HashMap<String,Object>> list;
    HashMap<String, Object> map;
    private List<String> listStr;
    @Override
    protected int getContentView() {
        return R.layout.ac_applyparkingreceipt;
    }

    /**是否全选*/
    boolean ifAllTrue = false;
    @Override
    protected void initView() {

        topBar_applyparkingreceipt.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE,TopBar.RIGHT_TV_BTN);
        topBar_applyparkingreceipt.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        if(ifAllTrue){
            topBar_applyparkingreceipt.setText(TopBar.RIGHT_TV_BTN, "取消全选");
        }else{
            topBar_applyparkingreceipt.setText(TopBar.RIGHT_TV_BTN, "全选");
        }
        topBar_applyparkingreceipt.setText(TopBar.TITLE, "停车订单");
        topBar_applyparkingreceipt.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        topBar_applyparkingreceipt.setRightOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listStr.clear();
                if(ifAllTrue){
                    for(int i=0;i<list.size();i++){
                        list.get(i).put("boolean", false);
                    }
                    ifAllTrue = false;
                    topBar_applyparkingreceipt.setText(TopBar.RIGHT_TV_BTN, "全选");
                }else{
                    for(int i=0;i<list.size();i++){
                        list.get(i).put("boolean", true);
                        listStr.add(list.get(i).get("name").toString());
                    }
                    ifAllTrue = true;
                    topBar_applyparkingreceipt.setText(TopBar.RIGHT_TV_BTN, "取消全选");
                }
                adapter.notifyDataSetChanged();
                Log.e("tag","已选择了:" + listStr.size() + "项");
                Log.e("tag","list:" + list.toString());
            }
        });

        listView.setDividerHeight(3);
    }


    @OnClick({R.id.tv_applyparkingreceipt_submit})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.tv_applyparkingreceipt_submit:
                startActivity(new Intent(ApplyParkingReceiptActivity.this,ApplyReceiptInfoActivity.class));
                break;
        }
    }
    @Override
    protected void initData() {
        list = new ArrayList<HashMap<String,Object>>();
        listStr = new ArrayList<String>();
        JSONObject Json;
        JSONArray JsonArray = new JSONArray();
        for (int i = 0; i < 20; i++) {
            Json = new JSONObject();
            try {
                Json.put("receipt_applyparkingorder_location","华清学府停车场"+i);
                Json.put("receipt_applyparkingorder_price","￥12：00");
                Json.put("receipt_applyparkingorder_carnum","陕A1111"+i);
                Json.put("receipt_applyparkingorder_time","3小时3分钟3秒");
                Json.put("receipt_applyparkingorder_start","进场时间：2018-03-03 12:12:12");
                Json.put("receipt_applyparkingorder_end","出场时间：2018-03-03 12:12:13");
                JsonArray.put(Json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Log.e("tag",JsonArray.toString());
        String  info;
        for (int i = 0; i < JsonArray.length(); i++) {
            map = new HashMap<String, Object>();
            info=  JsonArray.optJSONObject(i).toString();
            map.put("name",info);
            map.put("boolean", false);//初始化为未选
            list.add(map);
        }
        Log.e("tag","listView = "+list.toString());

        adapter = new ApplyParkingReceiptAdapter(this, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                ApplyParkingReceiptAdapter.ViewCache viewCache = (ApplyParkingReceiptAdapter.ViewCache) view.getTag();
                viewCache.cb_applyparkingreceipt.toggle();
                list.get(position).put("boolean", viewCache.cb_applyparkingreceipt.isChecked());
                adapter.notifyDataSetChanged();

                if(viewCache.cb_applyparkingreceipt.isChecked()){//被选中状态
                    listStr.add(list.get(position).get("name").toString());
                }else//从选中状态转化为未选中
                {
                    listStr.remove(list.get(position).get("name").toString());
                }

                Log.e("tag","已选择了:"+listStr.size()+"项");
            }
        });
        adapter.notifyDataSetChanged();
    }
}
